package com.todo.content.application.api.controller;

import com.todo.common.common.entity.response.BizResponse;
import com.todo.content.api.ContentInfoApi;
import com.todo.content.api.request.*;
import com.todo.content.api.response.ContentDetailResponse;
import com.todo.content.api.response.ContentInfoResponse;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentDetailBO;
import com.todo.content.application.entity.bo.ContentInfoBO;
import com.todo.content.application.service.ContentInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/2/24 02:38
 * @description
 */
@RestController
@RequestMapping("/api/contentInfo")
@Tag(name = "服务端-内容详情接口", description = "服务端-内容详情接口")
public class ContentInfoApiController implements ContentInfoApi {

    @Resource
    private ContentInfoService contentInfoService;

    @Override
    @Operation(summary = "保存预校验")
    @PostMapping("/savePreCheck")
    public BizResponse<Boolean> savePreCheck(@RequestBody SavePreCheckRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        ContentDetailBO contentDetailBO = ContentConvert.convertToContentDetailBO(request);
        Boolean check = contentInfoService.savePreCheck(contentDetailBO, true);
        return BizResponse.success(check);
    }

    @Override
    @Operation(summary = "新增内容详情")
    @PostMapping("/insertContentDetail")
    public BizResponse<Long> insertContentDetail(@RequestBody InsertContentDetailRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        ContentDetailBO contentDetailBO = ContentConvert.convertToContentDetailBO(request);
        Long contentId = contentInfoService.insertContentDetail(contentDetailBO);
        return BizResponse.success(contentId);
    }

    @Override
    @Operation(summary = "更新内容详情")
    @PostMapping("/updateContentDetail")
    public BizResponse<Boolean> updateContentDetail(@RequestBody UpdateContentDetailRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        ContentDetailBO contentDetailBO = ContentConvert.convertToContentDetailBO(request);
        Boolean updated = contentInfoService.updateContentDetail(contentDetailBO);
        return BizResponse.success(updated);
    }

    @Override
    @Operation(summary = "根据主键删除内容信息")
    @PostMapping("/removeContentDetailById")
    public BizResponse<Boolean> removeContentDetailById(@RequestBody RemoveContentDetailByIdRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        Boolean removed = contentInfoService.removeContentDetailById(request.getContentId());
        return BizResponse.success(removed);
    }

    @Override
    @Operation(summary = "根据主键查询内容详情")
    @PostMapping("/queryContentDetailById")
    public BizResponse<ContentDetailResponse> queryContentDetailById(@RequestBody QueryContentDetailByIdRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(request.getContentId());
        return BizResponse.success(ContentConvert.convertToContentDetailResponse(contentDetailBO));
    }

    @Override
    @Operation(summary = "根据Id列表查询内容信息")
    @PostMapping("/queryContentInfoByIdList")
    public BizResponse<List<ContentInfoResponse>> queryContentInfoByIdList(@RequestBody QueryContentInfoByIdListRequest request) {
        if (request == null || !request.baseCheck()) {
            return BizResponse.paramError();
        }
        List<ContentInfoBO> contentInfoBOS = contentInfoService.queryContentInfoByIdList(request.getContentIdList());
        if (CollectionUtils.isEmpty(contentInfoBOS)) {
            return BizResponse.success();
        }
        List<ContentInfoResponse> contentInfoResponseList = contentInfoBOS.stream()
                .map(ContentConvert::convertToContentInfoResponse)
                .collect(Collectors.toList());
        return BizResponse.success(contentInfoResponseList);
    }
}
